package com.hdy.framework.tag.paging.tags;
/**
 * 处理<h:paging-style type="1"/>
 */
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts2.ServletActionContext;

import com.hdy.framework.spring.entity.SpringContainer;
import com.hdy.framework.tag.paging.dto.PagingDto;
import com.hdy.framework.tag.paging.style.IStyleExcuter;
import com.hdy.framework.tag.paging.utils.PagingConstant;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;

public class PagingStyle extends BodyTagSupport{
	//选择那种方式
	private int type;
	private Object getAction(HttpServletRequest req) {
		String s = ServletActionContext.getServletContext().getRealPath("");
		ActionContext ctx = ServletActionContext.getActionContext(req);
		if (ctx != null) {
			ActionInvocation invocation = ctx.getActionInvocation();
			Object oo = invocation.getInvocationContext().getName();
			if (invocation != null)
				return invocation.getAction();
		}
		return null;
	}
	//获取当前 location.href的值
	private String getActionUrl(){
		//XXX XXX XXX 留着日后牛叉的时候做
		return "";
	}
	//考虑当前页 跟每页数量
	@Override
	public int doEndTag() throws JspException {
		System.out.println("--------------------");
		HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
		Map queryMap = (Map) req.getAttribute(PagingConstant.PAGING_REQUEST_KEY);//这里应该使用localThread暂时先这样
		PagingDto pd = (PagingDto) queryMap.get(PagingConstant.MAP_KEY_PAGING_PARAMS);
		IStyleExcuter excuter = null;
		if(!pd.isUseCountMethod()){
			excuter = SpringContainer.getBean("styleExcuterWithoutCnt", IStyleExcuter.class);
		}else{
			if(type == 0){//默认使用简单方式 无pCnt
				excuter = SpringContainer.getBean("styleExcuterWithoutCnt", IStyleExcuter.class);
			}else if(type==PagingConstant.PAGING_TYPE_WHIT_CNT){
				excuter = SpringContainer.getBean("styleExcuterWithCnt", IStyleExcuter.class);
			}else if(type==PagingConstant.PAGING_TYPE_WHITOUT_CNT){
				excuter = SpringContainer.getBean("styleExcuterWithoutCnt", IStyleExcuter.class);
			}
		}
		if(excuter==null){
			throw new RuntimeException("标签<*:paging-style type='*'/>未设置存在的type值 类型");
		}
		String outPageBar = excuter.buildPageBar(queryMap);
        JspWriter writer = pageContext.getOut();
        try {
            writer.print(outPageBar);

        } catch (Exception e) {
        }
        return (EVAL_PAGE);
	}
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
}
